package com.gbx.ximalayating.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.activity.BroadcastActivity;
import com.gbx.ximalayating.activity.MyGridView;
import com.gbx.ximalayating.adapter.BroadcastAdapter;
import com.gbx.ximalayating.application.BaseApplication;
import com.gbx.ximalayating.callback.OnImageViewClickListener;
import com.gbx.ximalayating.entity.AdsCEntity;
import com.gbx.ximalayating.entity.BroadcastAdsDataEntity;
import com.gbx.ximalayating.entity.CategoriesEntity;
import com.gbx.ximalayating.entity.LocalRadios;
import com.gbx.ximalayating.entity.TopRadios;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BroadcastFragment extends Fragment implements View.OnClickListener,OnImageViewClickListener,Callback<BroadcastAdsDataEntity>{


    private Button placeBtn, provinceBtn, countryBtn, netWorkBtn;
    private MyGridView gridView;
    private BroadcastAdapter broadcastAdapter;
    private List<LocalRadios> localRadios;
    private List<TopRadios> topRadios;
    private ImageView coverSmall, coverSmall1, coverSmall2, coverSmall3, coverSmall4, coverSmall5;
    private TextView name, name1, name2, name3, name4, name5;
    private TextView programName, programName1, programName2, programName3, programName4, programName5;
    private TextView playCount, playCount1, playCount2, playCount3, playCount4, playCount5;
    private List<CategoriesEntity> cates;
    private List<CategoriesEntity> cateEntities;


    public BroadcastFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_broadcast, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        placeBtn = (Button) view.findViewById(R.id.place_btn);
        provinceBtn = (Button) view.findViewById(R.id.province_btn);
        countryBtn = (Button) view.findViewById(R.id.country_btn);
        netWorkBtn = (Button) view.findViewById(R.id.netWork_btn);
        placeBtn.setOnClickListener(this);
        provinceBtn.setOnClickListener(this);
        countryBtn.setOnClickListener(this);
        netWorkBtn.setOnClickListener(this);


        gridView = ((MyGridView) view.findViewById(R.id.broadcast_gv));

        BaseApplication application = (BaseApplication) getActivity().getApplication();
        application.getBroadcastService().getBroadcastData()
                .enqueue(this);





        coverSmall = ((ImageView) view.findViewById(R.id.broadcast_local_include1).findViewById(R.id.broadcast_include_coverSmall));
        coverSmall1 = ((ImageView) view.findViewById(R.id.broadcast_local_include2).findViewById(R.id.broadcast_include_coverSmall));
        coverSmall2 = ((ImageView) view.findViewById(R.id.broadcast_local_include3).findViewById(R.id.broadcast_include_coverSmall));
        coverSmall3 = ((ImageView) view.findViewById(R.id.broadcast_top_include1).findViewById(R.id.broadcast_include_coverSmall));
        coverSmall4 = ((ImageView) view.findViewById(R.id.broadcast_top_include2).findViewById(R.id.broadcast_include_coverSmall));
        coverSmall5 = ((ImageView) view.findViewById(R.id.broadcast_top_include3).findViewById(R.id.broadcast_include_coverSmall));


        name = ((TextView) view.findViewById(R.id.broadcast_local_include1).findViewById(R.id.broadcast_include_name));
        name1 = ((TextView) view.findViewById(R.id.broadcast_local_include2).findViewById(R.id.broadcast_include_name));
        name2 = ((TextView) view.findViewById(R.id.broadcast_local_include3).findViewById(R.id.broadcast_include_name));
        name3 = ((TextView) view.findViewById(R.id.broadcast_top_include1).findViewById(R.id.broadcast_include_name));
        name4 = ((TextView) view.findViewById(R.id.broadcast_top_include2).findViewById(R.id.broadcast_include_name));
        name5 = ((TextView) view.findViewById(R.id.broadcast_top_include3).findViewById(R.id.broadcast_include_name));

        programName = ((TextView) view.findViewById(R.id.broadcast_local_include1).findViewById(R.id.broadcast_include_programName));
        programName1 = ((TextView) view.findViewById(R.id.broadcast_local_include2).findViewById(R.id.broadcast_include_programName));
        programName2 = ((TextView) view.findViewById(R.id.broadcast_local_include3).findViewById(R.id.broadcast_include_programName));
        programName3 = ((TextView) view.findViewById(R.id.broadcast_top_include1).findViewById(R.id.broadcast_include_programName));
        programName4 = ((TextView) view.findViewById(R.id.broadcast_top_include2).findViewById(R.id.broadcast_include_programName));
        programName5 = ((TextView) view.findViewById(R.id.broadcast_top_include3).findViewById(R.id.broadcast_include_programName));

        playCount = ((TextView) view.findViewById(R.id.broadcast_local_include1).findViewById(R.id.broadcast_include_playCount));
        playCount1 = ((TextView) view.findViewById(R.id.broadcast_local_include2).findViewById(R.id.broadcast_include_playCount));
        playCount2 = ((TextView) view.findViewById(R.id.broadcast_local_include3).findViewById(R.id.broadcast_include_playCount));
        playCount3 = ((TextView) view.findViewById(R.id.broadcast_top_include1).findViewById(R.id.broadcast_include_playCount));
        playCount4 = ((TextView) view.findViewById(R.id.broadcast_top_include2).findViewById(R.id.broadcast_include_playCount));
        playCount5 = ((TextView) view.findViewById(R.id.broadcast_top_include3).findViewById(R.id.broadcast_include_playCount));


    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.place_btn:
                intent.setClass(getContext(), BroadcastActivity.class);
                intent.putExtra("title","本地台");
                break;
            case R.id.province_btn:
                intent.setClass(getContext(), BroadcastActivity.class);
                intent.putExtra("title","国家台");
                break;
            case R.id.country_btn:
                intent.setClass(getContext(), BroadcastActivity.class);
                intent.putExtra("title","省市台");
                break;
            case R.id.netWork_btn:
                intent.setClass(getContext(), BroadcastActivity.class);
                intent.putExtra("title","网络台");
                break;
        }
        startActivity(intent);
    }


    @Override
    public void onImageViewClick(List<CategoriesEntity> categories) {
        List<CategoriesEntity> a = new ArrayList<>();
        a.addAll(categories);
        broadcastAdapter = new BroadcastAdapter(getContext(),a,cates,this);
        gridView.setAdapter(broadcastAdapter);
    }

    @Override
    public void onResponse(Call<BroadcastAdsDataEntity> call, Response<BroadcastAdsDataEntity> response) {
        BroadcastAdsDataEntity body = response.body();
        AdsCEntity data = body.getData();
        List<CategoriesEntity> cateEntities = new ArrayList<>();
        cates = data.getCategories();
        for (int i = 0; i < 8; i++) {
            cateEntities.add(cates.get(i));
        }
        broadcastAdapter = new BroadcastAdapter(getContext(),cateEntities,cates,this);
        gridView.setAdapter(broadcastAdapter);
        localRadios = data.getLocalRadios();
        topRadios = data.getTopRadios();
        Picasso.with(getContext()).load(localRadios.get(0).getCoverSmall()).into(coverSmall);
        Picasso.with(getContext()).load(topRadios.get(0).getCoverSmall()).into(coverSmall3);
        Picasso.with(getContext()).load(localRadios.get(1).getCoverSmall()).into(coverSmall1);
        Picasso.with(getContext()).load(topRadios.get(1).getCoverSmall()).into(coverSmall4);
        Picasso.with(getContext()).load(localRadios.get(2).getCoverSmall()).into(coverSmall2);
        Picasso.with(getContext()).load(topRadios.get(2).getCoverSmall()).into(coverSmall5);
        name.setText(localRadios.get(0).getName());
        name1.setText(localRadios.get(1).getName());
        name2.setText(localRadios.get(2).getName());
        name3.setText(topRadios.get(0).getName());
        name4.setText(topRadios.get(1).getName());
        name5.setText(topRadios.get(2).getName());
        programName.setText(localRadios.get(0).getProgramName());
        programName3.setText(topRadios.get(0).getProgramName());
        programName1.setText(localRadios.get(1).getProgramName());
        programName4.setText(topRadios.get(1).getProgramName());
        programName2.setText(localRadios.get(2).getProgramName());
        programName5.setText(topRadios.get(2).getProgramName());
        playCount.setText(((double) (localRadios.get(0).getPlayCount() / 10000)) + "万人");
        playCount3.setText(((double) (topRadios.get(0).getPlayCount() / 10000)) + "万人");
        playCount1.setText(((double) (localRadios.get(1).getPlayCount() / 10000)) + "万人");
        playCount4.setText(((double) (topRadios.get(1).getPlayCount() / 10000)) + "万人");
        playCount2.setText(((double) (localRadios.get(2).getPlayCount() / 10000)) + "万人");
        playCount5.setText(((double) (topRadios.get(2).getPlayCount() / 10000)) + "万人");
        Toast.makeText(getContext(), localRadios.size() + "", Toast.LENGTH_SHORT).show();
        Toast.makeText(getContext(), topRadios.size() + "", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Call<BroadcastAdsDataEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
